// HttpSocket.h: interface for the HttpSocket class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(HTTP_SOCKET_FOR_DOWNLOAD_H)
#define HTTP_SOCKET_FOR_DOWNLOAD_H

#include <string>
#include <winsock2.h>

class HttpSocket  
{
public:
    enum Status
    {
        HTTP_SOCKET_UNINIT = 0,
        HTTP_SOCKET_CONNECTED,
        HTTP_SOCKET_RESPONSED,
        HTTP_SOCKET_GOT_CONTENT
    };

    HttpSocket(std::string const server, std::string const object, 
        std::string const cookie, std::string const referer, long nFrom = 0, 
        long nTo = 0, int nServerType = 0, std::string port = "80", int timeOut = 500000);
    HttpSocket(std::string const url, std::string const cookie = "", std::string const referer = "", int timeOut = 500000);
    virtual ~HttpSocket();

    static void parseUrl(std::string const &url, std::string &service, std::string &server, \
        std::string &object, std::string &port);

    bool getContent(std::string &content) const;
    std::string getRequestHeader() const;
    std::string getResponseHeader() const;
    int getResponseField(std::string const &session, std::string &value) const;
    int getServerState() const;
    Status getState() const;

private:
    std::size_t _formatRequestHeader(std::string const &server, std::string const &object, 
        std::string const &cookie, std::string const &referer, long nFrom = 0, long nTo = 0, int nServerType = 0);
    bool _connect(char const *host);
    bool _recv(int size, std::string &content) const;
    bool _closeSocket();
    bool _sendRequest();
    bool _retrieveResponseHeader();

private:
    Status _status;
    std::string _requestHeader; 
    std::string _responseHeader; 
    SOCKET _socket;
    int _port;
    int _timeOut;
};

#endif 
